//
//  MSDayActivityController.m
//  觅尚
//
//  Created by No.8 on 2019/4/3.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSDayActivityController.h"
#import "MSSweepCodeCell.h"
#import "MSDetailedEvaluationController.h"
#import "MSSweepModel.h"

@interface MSDayActivityController ()<UITableViewDataSource,UITableViewDelegate>

/** tableView*/
@property(nonatomic,strong)UITableView *tableView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *drawRecordListArray;


/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *ceoRewardMoney;
/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *drawCount;


/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *drawPeoCount;

/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *rewardCount;

/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *rewardMoney;

/** ceoRewardMoney*/
@property(nonatomic,strong)NSString *rewardPeoCount;

@end

@implementation MSDayActivityController
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.drawRecordListArray = [NSMutableArray new];
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    
    [self ReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = backgroundcolor;
    
    
    [self tableView];
}
- (void)ReloadData{
    
    [self.drawRecordListArray removeAllObjects];
    NSDictionary * params = @{@"belongBusid":[WYCAccountTool unarchivebelongBusid],@"queryTime":@"day"};
    [HTTPRequest POST:MSEvaluaIncome params:params succeed:^(id responseObject) {
        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            self.ceoRewardMoney = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"ceoRewardMoney"]];
            self.drawCount = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"drawCount"]];
            self.drawPeoCount = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"drawPeoCount"]];
            self.rewardCount = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"rewardCount"]];
            self.rewardMoney = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"rewardMoney"]];
            self.rewardPeoCount = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"evaluaIncome"][@"rewardPeoCount"]];
            self.drawRecordListArray = [MSSweepModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"drawRecordList"]];
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - [self navBarBottom] - 150 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 返回组头部view的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 160 * px;
    
}
//返回每组头部view

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc]init];
    
    headerView.backgroundColor = WYCRGBColor(20, 33, 48);
    
    
    //图片
    UIImageView *CheckIMG = [[UIImageView alloc] initWithFrame:CGRectMake(60 * px, 60 * px, 40 * px, 40 * px)];
    [headerView addSubview:CheckIMG];
    
    
    //标题
    UILabel * Titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(CheckIMG.frame) + 10 * px, 50 * px, 230 * px, 60 * px)];
    Titlelabel.textColor = [UIColor whiteColor];
    Titlelabel.font = [UIFont boldSystemFontOfSize:SYRealValue(36 / 2)];
    [headerView addSubview:Titlelabel];
    
    
    
    //详情按钮
    UIButton * detailedButton = [[UIButton alloc] initWithFrame:CGRectMake( ScreenWidth - 270 * px, Titlelabel.y - 10 * px, Titlelabel.width, Titlelabel.height + 20 * px)];
    //设置边框的颜色
    [detailedButton.layer setBorderColor:WYCRGBColor(80, 112, 148).CGColor];
    detailedButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(24 / 2)];
    //设置边框的粗细
    [detailedButton.layer setBorderWidth:1.0];
    [detailedButton setTitleColor:WYCRGBColor(59, 84, 112) forState:UIControlStateNormal];
    detailedButton.layer.cornerRadius = detailedButton.height * 0.5;
    [headerView addSubview:detailedButton];
    
    
    
    if (section == 0) {
        
        CheckIMG.image = [UIImage imageNamed:@"pingjiashuju_icon"];
        [detailedButton setTitle:@"评价明细" forState:UIControlStateNormal];
        Titlelabel.text = @"评价数据";
        [detailedButton addTarget:self action:@selector(EvaluateButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }else if (section == 1) {
        CheckIMG.image = [UIImage imageNamed:@"choujiangshuju_icon"];
        Titlelabel.text = @"抽奖数据";
        detailedButton.hidden = YES;
        
    }else {
        CheckIMG.image = [UIImage imageNamed:@"huojiangjilu_icon"];
        Titlelabel.text = @"获奖记录";
        detailedButton.hidden = YES;
    }
    
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 500 * px;
    }else {
        return 250 * px;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSSweepCodeCell * cell = [MSSweepCodeCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0){
        
        UIView *CellView = [[UIView alloc]init];
        CellView.frame = CGRectMake(30 * px, 0, ScreenWidth - 60 * px, 500 * px);
        CellView.cornerRadius = 20 * px;
        CellView.backgroundColor = BackViewColor;
        [cell.contentView addSubview:CellView];
        
        
        
        /** 获评人数标题*/
        UILabel * BeEvaluatedTitle = [[UILabel alloc] initWithFrame:CGRectMake(CellView.width / 6, 50 * px, ScreenWidth / 6, 50 * px)];
        BeEvaluatedTitle.textColor = WYCRGBColor(109,134,161);;
        BeEvaluatedTitle.text = @"获评人数";
        BeEvaluatedTitle.textAlignment = NSTextAlignmentCenter;
        BeEvaluatedTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [CellView addSubview:BeEvaluatedTitle];
        
        
        /** 获评人数*/
        UILabel * BeEvaluatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(BeEvaluatedTitle.x, CGRectGetMaxY(BeEvaluatedTitle.frame) + 30 * px, BeEvaluatedTitle.width, 60 * px)];
        BeEvaluatedLabel.textColor = [UIColor whiteColor];
        if (self.rewardPeoCount == nil || self.rewardPeoCount == NULL || [self.rewardPeoCount isKindOfClass:[NSNull class]] || [[self.rewardPeoCount stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            BeEvaluatedLabel.text = @"0";
        }else{
           BeEvaluatedLabel.text = [NSString stringWithFormat:@"%@",self.rewardPeoCount];
        }
        
        BeEvaluatedLabel.textAlignment = NSTextAlignmentCenter;
        BeEvaluatedLabel.font = [UIFont systemFontOfSize:SYRealValue(48 / 2)];
        [CellView addSubview:BeEvaluatedLabel];
        
        
        
        /** 评价次数*/
        UILabel * EvaluationTimesTitle = [[UILabel alloc] initWithFrame:CGRectMake(CellView.width / 3 * 2, BeEvaluatedTitle.y, BeEvaluatedTitle.width, BeEvaluatedTitle.height)];
        EvaluationTimesTitle.textColor = BeEvaluatedTitle.textColor;
        EvaluationTimesTitle.text = @"评价次数";
        EvaluationTimesTitle.textAlignment = NSTextAlignmentCenter;
        EvaluationTimesTitle.font = BeEvaluatedTitle.font;
        [CellView addSubview:EvaluationTimesTitle];
        
        
        /** 获评人数标题*/
        UILabel * EvaluationTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(EvaluationTimesTitle.x, BeEvaluatedLabel.y, BeEvaluatedTitle.width, BeEvaluatedLabel.height)];
        EvaluationTimesLabel.textColor = BeEvaluatedLabel.textColor;
        if (self.rewardCount == nil || self.rewardCount == NULL || [self.rewardCount isKindOfClass:[NSNull class]] || [[self.rewardCount stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            EvaluationTimesLabel.text = @"0";
        }else{
            EvaluationTimesLabel.text = [NSString stringWithFormat:@"%@",self.rewardCount];
        }
        
        EvaluationTimesLabel.textAlignment = NSTextAlignmentCenter;
        EvaluationTimesLabel.font = BeEvaluatedLabel.font;
        [CellView addSubview:EvaluationTimesLabel];
        
        
        
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(60 * px, CGRectGetMaxY(EvaluationTimesLabel.frame) + 50 * px, CellView.width - 120 * px, 1)];
        line.backgroundColor = WYCRGBColor(55, 63, 84);
        [CellView addSubview:line];
        
        
        /** 总金额*/
        UILabel * TotalSumTitle = [[UILabel alloc] initWithFrame:CGRectMake(BeEvaluatedTitle.x, CGRectGetMaxY(line.frame) + 50 * px, BeEvaluatedTitle.width, BeEvaluatedTitle.height)];
        TotalSumTitle.textColor = BeEvaluatedTitle.textColor;
        TotalSumTitle.text = @"总金额";
        TotalSumTitle.textAlignment = NSTextAlignmentCenter;
        TotalSumTitle.font = BeEvaluatedTitle.font;
        [CellView addSubview:TotalSumTitle];
        
        
        /** 获评人数标题*/
        UILabel * TotalSumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(TotalSumTitle.frame) + 20 * px, CellView.width * 0.5, BeEvaluatedLabel.height)];
        TotalSumLabel.textColor = BeEvaluatedLabel.textColor;
        if (self.rewardMoney == nil || self.rewardMoney == NULL || [self.rewardMoney isKindOfClass:[NSNull class]] || [[self.rewardMoney stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            TotalSumLabel.text = @"0";
        }else{
            TotalSumLabel.text = [NSString stringWithFormat:@"￥%@",self.rewardMoney];
        }
        
        TotalSumLabel.textAlignment = NSTextAlignmentCenter;
        TotalSumLabel.font = BeEvaluatedLabel.font;
        [CellView addSubview:TotalSumLabel];
        
        
        
        /** 收益金额*/
        UILabel * AmountIncomeTitle = [[UILabel alloc] initWithFrame:CGRectMake(EvaluationTimesTitle.x, TotalSumTitle.y, BeEvaluatedTitle.width, BeEvaluatedTitle.height)];
        AmountIncomeTitle.textColor = FontColor;
        AmountIncomeTitle.text = @"收益金额";
        AmountIncomeTitle.textAlignment = NSTextAlignmentCenter;
        AmountIncomeTitle.font = BeEvaluatedTitle.font;
        [CellView addSubview:AmountIncomeTitle];
        
        
        /** 获评人数标题*/
        UILabel * AmountIncomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CellView.width * 0.5, TotalSumLabel.y, TotalSumLabel.width, TotalSumLabel.height)];
        AmountIncomeLabel.textColor = AmountIncomeTitle.textColor;
        if (self.ceoRewardMoney == nil || self.ceoRewardMoney == NULL || [self.ceoRewardMoney isKindOfClass:[NSNull class]] || [[self.ceoRewardMoney stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            AmountIncomeLabel.text = @"0";
        }else{
            AmountIncomeLabel.text = [NSString stringWithFormat:@"￥%@",self.ceoRewardMoney];
        }
//        AmountIncomeLabel.text = [NSString stringWithFormat:@"￥%@",self.ceoRewardMoney];
        AmountIncomeLabel.textAlignment = NSTextAlignmentCenter;
        AmountIncomeLabel.font = BeEvaluatedLabel.font;
        [CellView addSubview:AmountIncomeLabel];
    
    }else if (indexPath.section == 1){
        
        UIView *CellView = [[UIView alloc]init];
        CellView.frame = CGRectMake(30 * px, 0, ScreenWidth - 60 * px, 250 * px);
        CellView.cornerRadius = 20 * px;
        CellView.backgroundColor = BackViewColor;
        [cell.contentView addSubview:CellView];
        
        
        
        /** 获评人数标题*/
        UILabel * ParticipateTitle = [[UILabel alloc] initWithFrame:CGRectMake(CellView.width / 6, 50 * px, ScreenWidth / 4, 50 * px)];
        ParticipateTitle.textColor = WYCRGBColor(109,134,161);
        ParticipateTitle.text = @"参与抽奖人数";
        ParticipateTitle.textAlignment = NSTextAlignmentCenter;
        ParticipateTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [CellView addSubview:ParticipateTitle];
        
        
        /** 获评人数*/
        UILabel * ParticipateLabel = [[UILabel alloc] initWithFrame:CGRectMake(ParticipateTitle.x, CGRectGetMaxY(ParticipateTitle.frame) + 30 * px, ParticipateTitle.width, 60 * px)];
        ParticipateLabel.textColor = [UIColor whiteColor];
        if (self.drawPeoCount == nil || self.drawPeoCount == NULL || [self.drawPeoCount isKindOfClass:[NSNull class]] || [[self.drawPeoCount stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            ParticipateLabel.text = @"0";
        }else{
            ParticipateLabel.text = [NSString stringWithFormat:@"%@",self.drawPeoCount];
        }
        
        ParticipateLabel.textAlignment = NSTextAlignmentCenter;
        ParticipateLabel.font = [UIFont systemFontOfSize:SYRealValue(48 / 2)];
        [CellView addSubview:ParticipateLabel];
        
        
        
        /** 评价次数*/
        UILabel * ParticipateTimesTitle = [[UILabel alloc] initWithFrame:CGRectMake(CellView.width / 6 * 3.5, ParticipateTitle.y, ParticipateTitle.width, ParticipateTitle.height)];
        ParticipateTimesTitle.textColor = ParticipateTitle.textColor;
        ParticipateTimesTitle.text = @"参与抽奖次数";
        ParticipateTimesTitle.textAlignment = NSTextAlignmentCenter;
        ParticipateTimesTitle.font = ParticipateTitle.font;
        [CellView addSubview:ParticipateTimesTitle];
        
        
        /** 获评人数标题*/
        UILabel * ParticipateTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(ParticipateTimesTitle.x, ParticipateLabel.y, ParticipateLabel.width, ParticipateLabel.height)];
        ParticipateTimesLabel.textColor = ParticipateLabel.textColor;
        if (self.drawCount == nil || self.drawCount == NULL || [self.drawCount isKindOfClass:[NSNull class]] || [[self.drawCount stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            
            ParticipateTimesLabel.text = @"0";
        }else{
            ParticipateTimesLabel.text = [NSString stringWithFormat:@"%@",self.drawCount];
        }
        
        ParticipateTimesLabel.textAlignment = NSTextAlignmentCenter;
        ParticipateTimesLabel.font = ParticipateLabel.font;
        [CellView addSubview:ParticipateTimesLabel];
    }else{
        
        UIView *CellView = [[UIView alloc]init];
        CellView.frame = CGRectMake(30 * px, 0, ScreenWidth - 60 * px, 250 * px);
        CellView.cornerRadius = 20 * px;
        CellView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:CellView];
        
        CGFloat PrizeWinninglabelX = 30 * px;
        CGFloat PrizeWinninglabelH = 50 * px;
        CGFloat PrizeWinninglabelW = CellView.width * 0.7;
        
        for (int i = 0; i < self.drawRecordListArray.count; i++) {
            MSSweepModel * model = self.drawRecordListArray[i];
            /** 获奖信息*/
            UILabel * PrizeWinninglabel =  [[UILabel alloc] initWithFrame:CGRectMake(PrizeWinninglabelX, (PrizeWinninglabelH + 10) * i, PrizeWinninglabelW, PrizeWinninglabelH)];
            PrizeWinninglabel.textColor = WYCRGBColor(109,134,161);;
            PrizeWinninglabel.text = [NSString stringWithFormat:@"%@",model.userName];
            PrizeWinninglabel.textAlignment = NSTextAlignmentLeft;
            PrizeWinninglabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
            [CellView addSubview:PrizeWinninglabel];
        }
        
    }
    return cell;
    
}

- (void)EvaluateButtonClick{
        MSDetailedEvaluationController * DetaileVC = [[MSDetailedEvaluationController alloc] init];
        [self.navigationController pushViewController:DetaileVC animated:NO];
}

@end
